package com.example.administrator.juyanresturantapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ProductListActivity extends AppCompatActivity {
    ArrayList<Foods> foodList;
    ListView mLvFoods;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        
        initData();//获取数据
        initView();//初始化视图
        initEvent();//初始化事件
        
    }

    private void initEvent() {
    }

    private void initView() {
        mLvFoods = findViewById(R.id.lv_foods);

    }

    private void initData() {
        //获取数据
        // 1 创建一个请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(this);

// 2 创建一个请求
        String url = "http://182.61.26.165:8080/JuYanResturantApp/food/list";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("AppDebug", "onResponse: "+jsonObject.toString());
                try{
                    JSONArray list = jsonObject.getJSONArray("list");
                    //
                    foodList = new ArrayList();
                    for(int i=0;i<list.length();i++){
                        //
                        Foods foods = new Foods();
                        //设置食物信息
                        JSONObject object = list.getJSONObject(i);
                        foods.setId(object.getInt("id"));
                        foods.setName(object.getString("name"));
                        foods.setDescription(object.getString("description"));
                        foods.setLabel(object.getString("laber"));
                        foods.setPrice((float) object.getDouble("price"));
                        foods.setIcon(object.getString("icon"));
                        //
                        foodList.add(foods);
                        //

                    }
                    updateView();//

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("AppDebug", "onErrorResponse: "+volleyError);
            }
        });

// 3 将创建的请求添加到请求队列中
        requestQueue.add(jsonObjectRequest);


    }

    private void updateView() {
        //创建适配器对象

            //定义适配器类
        MyAdapter myAdapter = new MyAdapter();

        //绑定到ListView
        mLvFoods.setAdapter(myAdapter);
    }
    //定义适配器类
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return foodList.size();//要显示的数据数量
        }

        @Override
        public Object getItem(int position) {
            return foodList.get(position);//要显示的第position位置的数据
        }

        @Override
        public long getItemId(int position) {
            return position;//
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //数据项 和 显示的空间 要对应起来
            //获取数据
            Foods foods = foodList.get(position);
            //显示的控件
            View view = View.inflate(ProductListActivity.this,
                    R.layout.list_item_layout,
                    null);
            ImageView imageView = view.findViewById(R.id.iv_icon);//图片控件
            TextView tvName = view.findViewById(R.id.tv_name);
            //
            //数据和控件对应起来
            tvName.setText(foods.getName());
            //
            Picasso.with(ProductListActivity.this)
                    .load("http://p20.qhimgs3.com/sdm/240_240_70/t0102fd2890e1f8f226.jpg?t=1566248138")
                    .into(imageView);



            return view;//返回控件
        }
    }
}
